package com.sinosoft.cloud.access.abc.service;

import com.sinosoft.cloud.access.annotations.AccessService;
import com.sinosoft.cloud.access.service.AccessServiceBean;
import com.sinosoft.cloud.access.util.TimeUtil;
import com.sinosoft.cloud.cbs.fin.client.SendDueMsgService;
import com.sinosoft.cloud.cbs.uw.CalRiskAmntService;
import com.sinosoft.cloud.cbs.uw.UWCheckService;
import com.sinosoft.cloud.nb.app.client.*;
import com.sinosoft.cloud.rest.TradeInfo;
import com.sinosoft.lis.entity.LCContPojo;
import com.sinosoft.lis.entity.LCResultInfoPojo;
import feign.RetryableException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 新单试算业务处理类
 *
 * @author：崔广东
 */
@AccessService(name = "abc", transCode = "1002")
public class NewBusinessService extends AccessServiceBean {

    /**
     * 日志管理
     */
    private final Log cLogger = LogFactory.getLog(this.getClass());

    /*需要调用的微服务接口*/
    //前置规则校验
    @Autowired
    private CheckPreRuleService checkPreRuleService;
    //生成保单号微服务
    @Autowired
    private PolicyNoService policyNoService;
    //保单结构微服务
    @Autowired
    private ContInputService contInputService;
    //投保规则校验微服务
    @Autowired
    private CheckAppRuleService checkAppRuleService;
    //保费计算微服务
    @Autowired
    private MSPremCalService msPremCalService;
    //投保规则微服务
    @Autowired
    private CheckRiskRuleService checkRiskRuleService;
    //计算风险保额微服务
    @Autowired
    private CalRiskAmntService calRiskAmntService;
    //核保校验微服务
    @Autowired
    private UWCheckService uwCheckService;
    //契约落库微服务
    @Autowired
    private NBDataStoreService nbDataStoreService;
    //发送应收消息微服务
    @Autowired
    private SendDueMsgService sendDueMsgService;

    @Override
    public TradeInfo service(TradeInfo reqTradeInfo) {

        //错误信息
        String errorStr = "";
        String errorLog = "";

        /**
         * 调用微服务接口
         */
        TradeInfo respTradeInfo = null;

        long microServiceStart = 0L;
        long microServiceEnd = 0L;

        //调用前置规则校验微服务
        try {
            cLogger.info("1.开始调用前置规则校验微服务");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = checkPreRuleService.service(reqTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "前置规则校验微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "前置规则校验", e);
            return getErrorTradeInfo(errorStr);
        } catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "前置规则校验", e);
            return getErrorTradeInfo(errorStr);
        }

        if (respTradeInfo.getErrorList().size() == 0) {
            cLogger.info("调用前置规则校验微服务成功。返回结果是：" + respTradeInfo.toString());
        } else {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer(1024).append("前置规则校验微服务内部发生错误，")
                    .append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }

        //调用生成保单号微服务
        try {
            cLogger.info("2.开始调用生成保单号微服务");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = policyNoService.service(respTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "生成保单号微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "生成保单号", e);
            return getErrorTradeInfo(errorStr);
        } catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "生成保单号", e);
            return getErrorTradeInfo(errorStr);
        }
        LCContPojo mLCCont = (LCContPojo) respTradeInfo.getData(LCContPojo.class.getName());
        if (respTradeInfo.getErrorList().size() == 0 && null != mLCCont.getContNo() && !"".equals(mLCCont.getContNo())) {
            cLogger.info("调用生成保单号微服务成功。返回结果是：" + respTradeInfo.toString());
        } else {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer(1024).append("生成保单号微服务内部发生错误，")
                    .append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }

        //调用保单结构微服务
        try {
            cLogger.info("3.开始调用保单结构微服务");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = contInputService.service(respTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "保单结构微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "保单结构", e);
            return getErrorTradeInfo(errorStr);
        } catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "保单结构", e);
            return getErrorTradeInfo(errorStr);
        }
        if (respTradeInfo.getErrorList().size() == 0) {
            cLogger.info("调用保单结构微服务成功。返回结果是：" + respTradeInfo.toString());
        } else {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer(1024).append("保单结构微服务内部发生错误，")
                    .append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }

        //调用投保规则校验微服务
        try {
            cLogger.info("4.开始调用投保规则校验微服务");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = checkAppRuleService.service(respTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "投保规则校验微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "投保规则校验", e);
            return getErrorTradeInfo(errorStr);
        } catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "投保规则校验", e);
            return getErrorTradeInfo(errorStr);
        }
        if (respTradeInfo.getErrorList().size() == 0) {
            cLogger.info("调用投保规则校验微服务成功。返回结果是：" + respTradeInfo.toString());
        } else {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer(1024).append("投保规则校验微服务内部发生错误，")
                    .append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }
        //投保失败返回规则编码和失败信息
        List<LCResultInfoPojo> LCResultInfoList = (List<LCResultInfoPojo>) respTradeInfo.getData(LCResultInfoPojo.class.getName());
        if (null != LCResultInfoList && LCResultInfoList.size() > 0) {
            errorStr = new StringBuffer(1024).append("[").append(LCResultInfoList.get(0).getKey1())
                    .append("]:").append(LCResultInfoList.get(0).getResultContent()).toString();
            errorLog = "LCResultInfoList信息不为空，错误信息是：" + LCResultInfoList.toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }

        //调用保费计算微服务
        try {
            cLogger.info("5.开始调用保费计算微服务");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = msPremCalService.service(respTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "保费计算微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "保费计算", e);
            return getErrorTradeInfo(errorStr);
        } catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "保费计算", e);
            return getErrorTradeInfo(errorStr);
        }
        if (respTradeInfo.getErrorList().size() == 0) {
            cLogger.info("调用保费计算微服务成功。返回结果是：" + respTradeInfo.toString());
        } else {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer(1024).append("保费计算微服务内部发生错误，")
                    .append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }

        //调用投保规则微服务
        try {
            cLogger.info("6.开始投保规则微服务");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = checkRiskRuleService.service(respTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "投保规则微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "投保规则", e);
            return getErrorTradeInfo(errorStr);
        } catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "投保规则", e);
            return getErrorTradeInfo(errorStr);
        }
        if (respTradeInfo.getErrorList().size() == 0) {
            cLogger.info("调用投保规则微服务成功。返回结果是：" + respTradeInfo.toString());
        } else {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer(1024).append("投保规则微服务内部发生错误，")
                    .append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }
        //投保失败返回规则编码和失败信息
        LCResultInfoList = (List<LCResultInfoPojo>) respTradeInfo.getData(LCResultInfoPojo.class.getName());
        if (null != LCResultInfoList && LCResultInfoList.size() > 0) {
            errorStr = new StringBuffer(1024).append("[").append(LCResultInfoList.get(0).getKey1())
                    .append("]:").append(LCResultInfoList.get(0).getResultContent()).toString();
            errorLog = "LCResultInfoList信息不为空，错误信息是：" + LCResultInfoList.toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }

        //调用计算风险保额微服务
        try {
            cLogger.info("7.开始调用计算风险保额微服务");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = calRiskAmntService.service(respTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "计算风险保额微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "计算风险保额", e);
            return getErrorTradeInfo(errorStr);
        } catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "计算风险保额", e);
            return getErrorTradeInfo(errorStr);
        }
        if (respTradeInfo.getErrorList().size() == 0) {
            cLogger.info("调用计算风险保额微服务成功。返回结果是：" + respTradeInfo.toString());
        } else {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer(1024).append("调用计算风险保额微服务内部发生错误，")
                    .append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }

        //调用核保校验微服务
        try {
            cLogger.info("8.开始调用核保校验微服务");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = uwCheckService.service(respTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "核保校验微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "核保校验", e);
            return getErrorTradeInfo(errorStr);
        } catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "核保校验", e);
            return getErrorTradeInfo(errorStr);
        }
        //核保失败返回规则编码和失败信息
        LCResultInfoList = (List<LCResultInfoPojo>) respTradeInfo.getData(LCResultInfoPojo.class.getName());
        if (null != LCResultInfoList && LCResultInfoList.size() > 0) {
            errorStr = new StringBuffer(1024).append("[").append(LCResultInfoList.get(0).getResultNo())
                    .append("]:").append(LCResultInfoList.get(0).getResultContent()).toString();
            errorLog = "LCResultInfoList信息不为空，错误信息是：" + LCResultInfoList.toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }

        if (respTradeInfo.getErrorList().size() == 0) {
            cLogger.info("调用核保校验微服务成功。返回结果是：" + respTradeInfo.toString());
        } else {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer(1024).append("调用核保校验微服务内部发生错误，")
                    .append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }

        //调用契约落库微服务
        try {
            cLogger.info("9.开始调用契约落库微服务");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = nbDataStoreService.service(respTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "契约落库微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "契约落库", e);
            return getErrorTradeInfo(errorStr);
        } catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "契约落库", e);
            return getErrorTradeInfo(errorStr);
        }
        if (respTradeInfo.getErrorList().size() == 0) {
            cLogger.info("调用契约落库微服务成功。返回结果是：" + respTradeInfo.toString());
        } else {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer(1024).append("调用契约落库微服务内部发生错误，")
                    .append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }

        //调用发送应收微服务
        try {
            cLogger.info("10.开始调用发送应收消息微服务");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = sendDueMsgService.service(respTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "发送应收消息微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "发送应收消息", e);
            return getErrorTradeInfo(errorStr);
        } catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "发送应收消息", e);
            return getErrorTradeInfo(errorStr);
        }
        if (respTradeInfo.getErrorList().size() == 0) {
            cLogger.info("调用发送应收消息微服务成功。返回结果是：" + respTradeInfo.toString());
        } else {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer(1024).append("调用发送应收消息微服务内部发生错误，")
                    .append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }

        return respTradeInfo;
    }

}
